package member

import (
	"context"
	"errors"
	"group-buy/group_buy/internal/common"
	"group-buy/group_buy/internal/models"
	"group-buy/group_buy/internal/request_models"
	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RealNameLogic) RealName(req *types.UserInfo) (resp *types.UserInfo, err error) {
	member := &models.Member{Id: req.UserId}
	get, err := l.svcCtx.Engine.Get(member)
	if err != nil {
		return nil, err
	}
	if !get {
		return nil, errors.New("会员查询失败")
	}
	if len(member.LsCard) > 0 {
		return nil, errors.New("已实名认证成功,请勿重复操作")
	}
	userInfo := &models.LsUserInfo{
		Id:               req.Id,
		UserId:           req.UserId,
		CertType:         req.CertType,
		Name:             req.Name,
		CertNo:           req.CertNo,
		CertValidityType: req.CertValidityType,
		CertBeginDate:    req.CertBeginDate,
		CertEndDate:      req.CertEndDate,
		MobileNo:         req.MobileNo,
		Email:            req.Email,
	}

	lsUser := &request_models.LsUserInfo{
		CertType: userInfo.CertType,
		Name:     userInfo.Name,
		CertNo:   userInfo.CertNo,
		// CertValidityType: userInfo.CertValidityType,
		// CertBeginDate:    userInfo.CertBeginDate,
		// CertEndDate:      userInfo.CertEndDate,
		MobileNo: userInfo.MobileNo,
		Email:    userInfo.Email,
	}
	person := &request_models.LsPerson{
		MchNo:          l.svcCtx.Config.LsPay.MchNo,
		AppId:          l.svcCtx.Config.LsPay.AppId,
		OrderId:        common.GetStrId(),
		ChannelExtra:   &request_models.UserInfo{UserInfo: lsUser},
		WayCode:        "APPLY",
		RelationType:   "USER",
		AccountName:    userInfo.Name,
		AccountType:    1,
		AccountAlias:   userInfo.Name,
		DivisionProfit: "0.0",
		ReqTime:        common.GetIntId(),
		Version:        "1.0",
		SignType:       "MD5",
	}
	rsp, err := person.Apply(l.svcCtx.Config.LsPay.PayKey)
	if err != nil {
		return nil, err
	}
	if rsp.Status != 200 {
		return nil, errors.New(rsp.Msg)
	}
	if len(rsp.Data.AccountNo) == 0 {
		return nil, errors.New(rsp.Data.ErrMsg)
	}
	userInfo.ChannelUser = rsp.Data.AccountNo
	req.ChannelUser = rsp.Data.AccountNo
	_, err = l.svcCtx.Engine.Insert(userInfo)
	if err != nil {
		return nil, err
	}
	_, _ = l.svcCtx.Engine.ID(req.UserId).Cols("ls_user").Update(&models.Member{LsUser: userInfo.ChannelUser})

	cardInfo := &models.LsCardInfo{
		Id:     req.Id,
		UserId: req.UserId,
		// CardType:         req.CardType,
		CardName: req.Name,
		CardNo:   req.CardNo,
		// ProvId:           req.ProvId,
		// AreaId:           req.AreaId,
		// BranchCode:       req.BranchCode,
		// BranchName:       req.BranchName,
		CertNo: req.CertNo,
		// CertValidityType: req.CertValidityType,
		// CertBeginDate:    req.CertBeginDate,
		// CertEndDate:      req.CertEndDate,
		Mp: req.MobileNo,
		// CardId:           req.CardId,
	}
	lsCard := &request_models.LsCardInfo{
		CardType: 1,
		CardName: cardInfo.CardName,
		CardNo:   cardInfo.CardNo,
		// ProvId:           cardInfo.ProvId,
		// AreaId:           cardInfo.AreaId,
		// BankCode:         req.BankCode,
		// BranchCode:       cardInfo.BranchCode,
		// BranchName:       cardInfo.BranchName,
		// CertType:         req.CertType,
		CertNo: cardInfo.CertNo,
		// CertValidityType: cardInfo.CertValidityType,
		// CertBeginDate:    cardInfo.CertBeginDate,
		// CertEndDate:      cardInfo.CertEndDate,
		Mp: cardInfo.Mp,
	}
	// cash :=make([]*request_models.CashInfo,1,1)
	// cash[0]=&request_models.CashInfo{CashType: "D1", SwitchState: 1, OutFeeFlag: 1}
	extra := &request_models.LsConfirmExtra{
		CardInfo:     lsCard,
		SettleConfig: &request_models.SettleInfo{SettleCycle: "D1"},
		// CashInfo:    cash,
	}
	confirm := &request_models.LsConfirm{
		MchNo:        l.svcCtx.Config.LsPay.MchNo,
		AppId:        l.svcCtx.Config.LsPay.AppId,
		OrderId:      common.GetStrId(),
		WayCode:      "APPLY",
		AccountNo:    userInfo.ChannelUser,
		ChannelExtra: extra,
		ReqTime:      common.GetIntId(),
		Version:      "1.0",
		SignType:     "MD5",
	}
	confirmRsp, confirmErr := confirm.Confirm(l.svcCtx.Config.LsPay.PayKey)
	if confirmErr != nil {
		return nil, err

	}

	if confirmRsp.Status == 200 {
		if len(confirmRsp.Data.AccountNo) > 0 {
			cardInfo.CardId = confirmRsp.Data.AccountNo
			// req.CardId = confirmRsp.Data.AccountNo
			_, err = l.svcCtx.Engine.Insert(cardInfo)
			if err != nil {
				return nil, err
			}
			member := &models.Member{LsCard: cardInfo.CardId}
			_, _ = l.svcCtx.Engine.ID(req.UserId).Cols("ls_card").Update(member)
		} else {
			return nil, errors.New(confirmRsp.Data.ErrMsg)
		}
	} else {
		return nil, errors.New(confirmRsp.Msg)
	}
	return req, nil
}
